package com.yupi.yuojcodesandbox.utils;

import com.yupi.yuojcodesandbox.model.ExecuteMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.StopWatch;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 进程工具类  -- 对控制台进行操作的封装（通用性很强，可以封装为一个jdk）
 *
 * @author wangjia
 */
public class ProcessUtils {
    /**
     * 执行进程并获取执行结果信息
     *
     * @param runPrecess
     * @return
     * @Param opName
     */
    public static ExecuteMessage runProcessAndGetMessage(Process runPrecess, String opName) {
        ExecuteMessage executeMessage = new ExecuteMessage();

        try {
            //Spring中监测时间的小工具
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();

            //等待程序执行，获取错误码
            int exitValue = runPrecess.waitFor();
            executeMessage.setExitValue(exitValue);
            //正常退出
            if (exitValue == 0) {
                System.out.println(opName + "成功");

                //分批获取进程的正常输出
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(runPrecess.getInputStream()));
                List<String> outputStrList = new ArrayList<>();
                //逐行读取
                String compileOutputLine;
                while ((compileOutputLine = bufferedReader.readLine()) != null) {
                    outputStrList.add(compileOutputLine);
                }
                //设置正常返回信息
                executeMessage.setMessage(StringUtils.join(outputStrList,"\n"));

            } else {
                //异常退出
                System.out.println(opName + "错误码：" + exitValue);
                //分批获取进程的正常输出
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(runPrecess.getInputStream()));
                List<String> outputStrList = new ArrayList<>();
                //逐行读取
                String compileOutputLine;
                while ((compileOutputLine = bufferedReader.readLine()) != null) {
                    outputStrList.add(compileOutputLine);
                }
                //设置正常返回信息
                executeMessage.setMessage(StringUtils.join(outputStrList,"\n"));


                //对错误信息输出
                //分批获取进程的正常输出
                BufferedReader errorbufferedReader = new BufferedReader(new InputStreamReader(runPrecess.getInputStream()));
                List<String> errorOutputStrList = new ArrayList<>();
                //逐行读取
                String errorcompileOutputLine;
                while ((compileOutputLine = bufferedReader.readLine()) != null) {
                    errorOutputStrList.add(compileOutputLine);
                }
                //设置正常返回信息
                executeMessage.setErrorMessage(StringUtils.join(errorOutputStrList,"\n"));


            }
            stopWatch.stop();
            executeMessage.setTime(stopWatch.getLastTaskTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return executeMessage;

    }


    /**
     * (参考，了解)
     * 执行交互式进程并获取执行结果信息
     *
     * @param runPrecess
     * @return
     * @Param opName
     */
    public static ExecuteMessage runInteractProcessAndGetMessage(Process runPrecess, String opName) {
        ExecuteMessage executeMessage = new ExecuteMessage();


        return executeMessage;
    }


}
